package io.api.modules.project.controller;

import com.google.common.collect.ImmutableBiMap;
import com.google.common.collect.Maps;
import io.admin.common.constant.Constant;
import io.admin.common.page.PageData;
import io.admin.common.utils.Result;
import io.admin.common.validator.ValidatorUtils;
import io.admin.common.validator.group.AddGroup;
import io.admin.common.validator.group.DefaultGroup;
import io.admin.common.validator.group.UpdateGroup;
import io.api.annotation.LoginUser;
import io.api.enums.ProjectRole;
import io.api.enums.ProjectStatus;
import io.api.enums.UserRole;
import io.api.modules.login.entity.UserEntity;
import io.api.modules.project.dto.ProjectDTO;
import io.api.modules.project.dto.UserProjectDTO;
import io.api.modules.project.service.ProjectService;
import io.api.modules.project.service.UserProjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.*;


/**
 * 项目表
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2024-07-12
 */
@RestController
@RequestMapping("/api/project")
@Api(tags="项目表")
public class ProjectController {
    @Autowired
    private ProjectService projectService;
    @Autowired
    private UserProjectService userProjectService;

    @GetMapping("page")
    @ApiOperation("分页")
    @ApiImplicitParams({
        @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
        @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String")
    })
    public Result<PageData<ProjectDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params, @ApiIgnore @LoginUser UserEntity user){
        if (user.getUserrole() != UserRole.MANAGER.getValue()) {
            params.put("userId", user.getId());
        }
        PageData<ProjectDTO> page = projectService.page(params);
        return new Result<PageData<ProjectDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @ApiOperation("信息")
    public Result<ProjectDTO> get(@PathVariable("id") Long id){
        ProjectDTO data = projectService.get(id);
        return new Result<ProjectDTO>().ok(data);
    }

    @PostMapping
    @ApiOperation("保存")
    public Result save(@RequestBody ProjectDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        projectService.save(dto);

        return new Result();
    }

    @PutMapping
    @ApiOperation("修改")
    public Result update(@RequestBody ProjectDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        projectService.update(dto);

        return new Result();
    }

    @GetMapping("/statistic")
    public Result statistic(@LoginUser UserEntity user){
        Long userId = null;
        if (user.getUserrole() != UserRole.MANAGER.getValue()) {
            userId = user.getId();
        }
        // 获取施工中的项目数
        int count1 = projectService.countByUserIdAndStatus(userId, Arrays.asList(ProjectStatus.WORKING.ordinal(),ProjectStatus.EXAMINE.ordinal()));
        // 获取已验收的项目数
        int count2 = projectService.countByUserIdAndStatus(userId, Collections.singletonList(ProjectStatus.FINISHED.ordinal()));

        Map<String, Integer> result = new HashMap<>();
        result.put("working", count1);
        result.put("finish", count2);
        return new Result().ok(result);

    }

    @PostMapping("examine/{projectId}")
    @ApiOperation("申请竣工验收")
    public Result examine(@PathVariable Long projectId, @LoginUser UserEntity user){
        UserProjectDTO projectUser = userProjectService.selectByProjectIdAndUserId(projectId, user.getId());
        if (Objects.nonNull(projectUser) && projectUser.getProjectRole().equals(ProjectRole.HEAD.getValue())) {
            projectService.examine(projectId);
            return new Result();
        }
        return new Result().error("没有权限");
    }


}